<?php
/* ******************************
	Incoming Tasks aJax actions
   ******************************
*/
	include('../../includes/classes/core.php');
	$action = $_REQUEST['act'];
	$start		=	$_REQUEST['start'];
    $end		=	$_REQUEST['end'];

	switch ($action) {
	    case 'get_list':
    	    $count = $_REQUEST['count'];

    	    $rResult = mysql_query("SELECT 	client.id,
											client.`name`,
											sum(IF(warehouse_detail.type = 2, 0, IFNULL(cartridge_pricelist.price,0)))+
											sum(IF(warehouse_detail.type = 2, 0, IFNULL(printer_pricelist.price,0))) AS SUM1
									FROM client
									left JOIN client_objects ON client_objects.client_id=client.id
									left JOIN a_letter ON client_objects.id = a_letter.client_object_id AND (MONTH(a_letter.fact_end_date) >= MONTH(CURDATE()))-3
									left JOIN d_letter ON client_objects.id = d_letter.client_object_id AND (MONTH(d_letter.fact_end_date) >= MONTH(CURDATE()))-3
									left JOIN warehouse ON warehouse.a_letter_id=a_letter.id
									LEFT JOIN warehouse as wh ON wh.d_letter_id=d_letter.id
									LEFT JOIN warehouse_detail ON warehouse_detail.warehouse_id =IFNULL(wh.id,warehouse.id) AND warehouse_detail.actived=1
									LEFT JOIN  warehouse_producing ON warehouse_producing.warehouse_detail_id = warehouse_detail.id
									LEFT JOIN cartridge_pricelist ON 	client_objects.client_id = cartridge_pricelist.client_id AND warehouse_producing.cartridge_part_id = cartridge_pricelist.parts_type 	AND cartridge_pricelist.production_id = warehouse_detail.prod_id
									LEFT JOIN printer_pricelist   ON 	client_objects.client_id = printer_pricelist.client_id 	AND warehouse_producing.printer_part_id    = printer_pricelist.parts_type 		AND printer_pricelist.production_id 	= warehouse_detail.prod_id
									GROUP BY client.`name`
									HAVING SUM1=0.00
											");

			$output = array(
				"aaData"	=> array()
			);

			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count ; $i++ )
				{
					/* General output */
					$row[] = $aRow[$i];
				}
				$output['aaData'][] = $row;
			}

			echo json_encode( $output );

	        break;
	    case 'chart':
	    	$rResult = mysql_query("SELECT 	DATE_FORMAT(IFNULL(d_letter.fact_end_date,a_letter.fact_end_date),'%b') AS mont,
														sum(IF(warehouse_detail.type = 2, 0, IFNULL(cartridge_pricelist.price,0)))+
														sum(IF(warehouse_detail.type = 2, 0, IFNULL(printer_pricelist.price,0)))
										FROM warehouse
										LEFT JOIN a_letter ON a_letter.id = warehouse.a_letter_id
										LEFT JOIN d_letter ON d_letter.id = warehouse.d_letter_id
										LEFT JOIN client_objects ON client_objects.id=IFNULL(d_letter.client_object_id,a_letter.client_object_id) AND client_objects.client_id=$_REQUEST[id]
										LEFT JOIN warehouse_detail ON warehouse.id = warehouse_detail.warehouse_id AND warehouse_detail.actived=1
										LEFT JOIN warehouse_producing ON warehouse_producing.warehouse_detail_id = warehouse_detail.id
										LEFT JOIN cartridge_pricelist ON 	client_objects.client_id = cartridge_pricelist.client_id AND warehouse_producing.cartridge_part_id = cartridge_pricelist.parts_type 	AND cartridge_pricelist.production_id = warehouse_detail.prod_id
										LEFT JOIN printer_pricelist   ON 	client_objects.client_id = printer_pricelist.client_id 	AND warehouse_producing.printer_part_id    = printer_pricelist.parts_type 		AND printer_pricelist.production_id 	= warehouse_detail.prod_id
										GROUP BY mont
										ORDER BY IFNULL(d_letter.fact_end_date,a_letter.fact_end_date) ");

	    	$output = array(
	    			"aaData"	=> array()
	    	);
	    	$data = array();
	    	while ( $aRow = mysql_fetch_array( $rResult ) )
	    	{

	    			$data[xAxis][categories][] = $aRow[0];
	    			$data[series][0][data][] = (float) $aRow[1];

	    	}

	    	echo json_encode( $data );
	    	break;

	    default:
	       	echo "null";
	}
?>